<template>
  <a-layout-header :class="[headerTheme, 'admin-header']">
    <div :class="['admin-header-wide', layout, pageWidth]">
      <router-link v-if="isMobile || layout === 'head'" to="/" :class="['logo', isMobile ? null : 'pc', headerTheme]">
        <img width="32" src="@/assets/img/logo.png"/>
        <h1 v-if="!isMobile" style="padding: 0 12px;">{{systemName}}</h1>
      </router-link>
      <a-tooltip v-if="!isMobile" class="header-item" :title="collapsed ? $t('title.spread') : $t('title.fold')" placement="bottom" >
        <a-icon v-if="layout !== 'head'" class="trigger" :type="collapsed ? 'menu-unfold' : 'menu-fold'" @click="toggleCollapse"/>
      </a-tooltip>
      <div v-if="layout !== 'side' && !isMobile" class="admin-header-menu" :style="`width: ${menuWidth};`">
        <i-menu class="head-menu" :theme="headerTheme" mode="horizontal" :options="menuData" @select="onSelect"/>
      </div>
      <div :class="['admin-header-right', headerTheme]">
          <a-tooltip class="header-item" :title="$t('title.documentation')" placement="bottom" >
            <a href="https://iczer.gitee.io/vue-antd-admin-docs/" target="_blank">
              <a-icon type="question-circle-o" />
            </a>
          </a-tooltip>
          <header-avatar class="header-item" @showChangePasswordModal="showChangePasswordModal"/>
          <select-lang></select-lang>
      </div>
    </div>
    <change-password-modal
      ref="changePasswordModal"
      :visible="visibleChangePasswordModal"
      @cancel="hideChangePasswordModal"
      @ok="changePasswordSuccess"
    />
  </a-layout-header>
</template>

<script>
import HeaderAvatar from './header-avatar'
import IMenu from '@/components/menu/menu'
import SelectLang from '@/components/lang/select-lang'
import {mapState} from 'vuex'
import ChangePasswordModal from './change-password-modal'

export default {
  name: 'AdminHeader',
  i18n: require('./i18n'),
  components: {IMenu, HeaderAvatar, SelectLang, ChangePasswordModal},
  props: ['collapsed', 'menuData'],
  data() {
    return {
      searchActive: false,
      visibleChangePasswordModal: false
    }
  },
  computed: {
    ...mapState('setting', ['theme', 'isMobile', 'layout', 'systemName', 'pageWidth']),
    systemName () {
        return this.$t(this.$store.state.setting.systemName)
    },
    headerTheme () {
      if (this.layout == 'side' && this.theme.mode == 'dark' && !this.isMobile) {
        return 'light'
      }
      return this.theme.mode
    },
    menuWidth() {
      const {layout, searchActive} = this
      const headWidth = layout === 'head' ? '100% - 188px' : '100%'
      const extraWidth = searchActive ? '600px' : '400px'
      return `calc(${headWidth} - ${extraWidth})`
    }
  },
  methods: {
    toggleCollapse () {
      this.$emit('toggleCollapse')
    },
    onSelect (obj) {
      this.$emit('menuSelect', obj)
    },
    changePasswordSuccess() {
      this.$message.success('修改密码成功')
      this.hideChangePasswordModal()
    },
    showChangePasswordModal() {
      this.visibleChangePasswordModal = true
    },
    hideChangePasswordModal() {
      this.visibleChangePasswordModal = false
      this.$refs.changePasswordModal.$refs.form.resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
@import "index";
</style>
